/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GUIChamadosDetalhes.java
 *
 * Created on 15/06/2011, 08:56:26
 */

package gui;

import dao.DAOChamadoDet;
import dao.ResultSetTableModel;
import javax.swing.JOptionPane;
import model.ChamadoDet;

/**
 *
 * @author Proprietário
 */
public class GUIChamadosDetalhes extends javax.swing.JFrame {

    String cliente;
    int protocolo;

    DAOChamadoDet chamadoDetDAO = new DAOChamadoDet();
    ChamadoDet detalheChamado = new ChamadoDet();

    /** Creates new form GUIChamadosDetalhes */
    public GUIChamadosDetalhes() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jpnResumo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jlbNomeCliente = new javax.swing.JLabel();
        jlbCodProtocolo = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbChamadosDetalhes = new javax.swing.JTable();
        jbtAdicionar = new javax.swing.JButton();
        jbtAlterar = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jbtFechar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Detalhes do chamado");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jpnResumo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Cliente:");

        jLabel2.setText("Protocolo:");

        jlbNomeCliente.setText("...");

        jlbCodProtocolo.setText("...");

        javax.swing.GroupLayout jpnResumoLayout = new javax.swing.GroupLayout(jpnResumo);
        jpnResumo.setLayout(jpnResumoLayout);
        jpnResumoLayout.setHorizontalGroup(
            jpnResumoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnResumoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnResumoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jpnResumoLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlbNomeCliente))
                    .addGroup(jpnResumoLayout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jlbCodProtocolo)))
                .addContainerGap(456, Short.MAX_VALUE))
        );
        jpnResumoLayout.setVerticalGroup(
            jpnResumoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jpnResumoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jpnResumoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jlbNomeCliente))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jpnResumoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jlbCodProtocolo))
                .addContainerGap(15, Short.MAX_VALUE))
        );

        jScrollPane1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusGained(java.awt.event.FocusEvent evt) {
                jScrollPane1FocusGained(evt);
            }
        });

        jtbChamadosDetalhes.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane1.setViewportView(jtbChamadosDetalhes);

        jbtAdicionar.setText("Adicionar");
        jbtAdicionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAdicionarActionPerformed(evt);
            }
        });

        jbtAlterar.setText("Alterar");
        jbtAlterar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtAlterarActionPerformed(evt);
            }
        });

        jbtFechar.setText("Fechar");
        jbtFechar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtFecharActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 537, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 452, Short.MAX_VALUE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jbtAlterar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jbtAdicionar, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                    .addComponent(jpnResumo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jbtFechar, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jpnResumo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jbtAdicionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jbtAlterar))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 265, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbtFechar)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    /**
     * inicializa dados na tela de detalhes
     */
    private void setDados() {
        jlbNomeCliente.setText(cliente);
        jlbCodProtocolo.setText(String.valueOf(protocolo));
    }

    /**
     * recebe o nome de um cliente para poder setar na tela de detalhe
     * @param pNomeCliente
     */
    public void setNomeCliente(String pNomeCliente) {
        cliente = pNomeCliente;
    }

    /**
     * recebe o codigo do cliente para poder setar na tela de detalhe
     * @param pCodCliente
     */
    public void setCodProtocolo(int pCodProtocolo) {
        protocolo = pCodProtocolo;
    }

    private void jbtFecharActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtFecharActionPerformed
        this.dispose();
    }//GEN-LAST:event_jbtFecharActionPerformed

    private void jbtAlterarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAlterarActionPerformed
        try {
            detalheChamado = chamadoDetDAO.getChamadoDetPorCodigo((Integer) jtbChamadosDetalhes.getValueAt(jtbChamadosDetalhes.getSelectedRow(), 0));

            GUIChamadosDetalhesCadastro chamadosDetalhesCadastro = new GUIChamadosDetalhesCadastro();
            chamadosDetalhesCadastro.setDetalhe(detalheChamado);
            chamadosDetalhesCadastro.setVisible(true);

        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, "Não é possível editar o registro selecionado.");
        }
    }//GEN-LAST:event_jbtAlterarActionPerformed

    private void jScrollPane1FocusGained(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_jScrollPane1FocusGained
        // TODO add your handling code here:
    }//GEN-LAST:event_jScrollPane1FocusGained

    private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
        setDados();
        atualizaTabela();
    }//GEN-LAST:event_formWindowGainedFocus

    private void jbtAdicionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtAdicionarActionPerformed
        try {
            detalheChamado = chamadoDetDAO.getChamadoDetPorCodigo(0);

            //define aqui o codigo de cabecalho do novo registro
            detalheChamado.setCodChamado(protocolo);

            // continua abrindo
            GUIChamadosDetalhesCadastro chamadosDetalhesCadastro = new GUIChamadosDetalhesCadastro();
            chamadosDetalhesCadastro.setDetalhe(detalheChamado);
            chamadosDetalhesCadastro.setVisible(true);

        } catch (Exception e) {

        }

    }//GEN-LAST:event_jbtAdicionarActionPerformed

    /**
     * Monta uma tabela especifica baseada no banco de cabecalho de chamados
     */
    private void atualizaTabela(){
        try {
            String[] TitulosColunas = {"Codigo", "Data", "Hora", "Descrição", "Encarregado"};

            jtbChamadosDetalhes.setModel(new ResultSetTableModel(chamadoDetDAO.getListaChamadoDet(protocolo), TitulosColunas));
            jtbChamadosDetalhes.updateUI();
            jtbChamadosDetalhes.repaint();
        } catch (Exception e) {
            System.out.println("erro: "+e.getMessage());
        }
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GUIChamadosDetalhes().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton jbtAdicionar;
    private javax.swing.JButton jbtAlterar;
    private javax.swing.JButton jbtFechar;
    private javax.swing.JLabel jlbCodProtocolo;
    private javax.swing.JLabel jlbNomeCliente;
    private javax.swing.JPanel jpnResumo;
    private javax.swing.JTable jtbChamadosDetalhes;
    // End of variables declaration//GEN-END:variables

}
